<?php

class userModel extends Model
{
    protected $_validate = array(
        array('userName', 'require', '{%parameterNone}'), //不能为空
        array('email', 'require', '{%parameterNone}'), //不能为空
        array('passWord', 'require', '{%parameterNone}'), //不能为空
        //array('repassWord', 'passWord', '{%inconsistent_passWord}', 0, 'confirm'), //确认密码
        array('email', 'email', '{%emailIError}'), //邮箱格式
        array('userName', '1,20', '{%userNameLengthError}', 0, 'length', 1), //用户名长度
        array('passWord', '3,20', '{%passWordLengthError}', 0, 'length', 1), //密码长度
        array('userName', '', '{%userNameIsUser}', 0, 'unique', 1), //新增的时候检测重复
        array('email', '', '{%emailIsUser}', 0, 'unique', 1), //新增的时候检测重复
    );

    protected $_auto = array(
        //array('passWord','md5',1,'function'), //密码加密
        array('regTime','time',1,'function'), //注册时间
        array('regIp','get_client_ip',1,'function'), //注册IP
    );




    public function GetUserId($con,$type='userName'){
    	if($type=='userName'){
	    	$res = $this->where("userName like '%$con%'")->field('id')->select();
	    	$arr = array();
	    	foreach($res as $k=>$v){
	    		$arr[] = $v['id'];
	    	}
	    	return $arr;
    	}else if($type=='token'){
    		$uid = $this->where(array('token'=>$con))->getField('id');
    		$uid = intval($uid);
    		return $uid;
    	}
    }





    /**
     * 修改用户名
     */
    public function rename($map, $newname) {
        if ($this->where(array('userName'=>$newname))->count('id')) {
            return false;
        }
        $this->where($map)->save(array('userName'=>$newname));
        $uid = $this->where(array('userName'=>$newname))->getField('id');
        //修改商品表中的用户名
        M('item')->where(array('uid'=>$uid))->save(array('uname'=>$newname));
        //修改专辑表中的用户名
        M('album')->where(array('uid'=>$uid))->save(array('uname'=>$newname));
        //评论和微薄暂时不修改。
        return true;
    }

    public function name_exists($name, $id = 0) {
        $where = "userName='" . $name . "' AND id<>'" . $id . "'";
        $result = $this->where($where)->count('id');
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    public function email_exists($email, $id = 0) {
        $where = "email='" . $email . "' AND id<>'" . $id . "'";
        $result = $this->where($where)->count('id');
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
    
  
    public function GetUserName($uid){
    	return $this->where(array('id'=>$uid))->getField('userName');
    }
    
   
    
    
    /**
     * 更新用户设置
     * @param unknown_type $date
     */
    public function SetUserSeting($date){
    	if(empty($date)){
    		return -1;
    	}  	
    	//print_r($date);
    	foreach($date as $k=>$v){
    		$v = trim($v);
    		//echo 'dd'.$v.'dd';
    		//echo 
    		if(null==$v){
    			unset($date[$k]);
    		}
    		
    	}
    	//print_r($date);
    	if(false!==$this->where(array('token'=>$date['token']))->save($date)){   	
    		//echo $this->getLastSql();
    		return 1;
    	}else{
    		//echo $this->getLastSql();
    		return 0;
    	}
    }
    /**
     * 获取一个用户的设置信息
     * @param unknown_type $token
     */
    public function GetUserSetting($token){    	
    	$list = $this->where(array('token'=>$token))->find();
    	//print_r($list);
    	$date['like']   =empty($list['like_notice'])?0:1;
    	$date['message']   =empty($list['msg_notice'])?0:1;
    	$date['moods']   =empty($list['moods_notice'])?0:1;
    	$date['follow']   =empty($list['follow_notice'])?0:1;
    	$date['otherMsg']   =empty($list['other_msg_notice'])?0:1;
    	$date['sinaWeiboShare']   =empty($list['sina_weibo_share'])?0:1;
    
    	return $date;
    }
   // public function 
   
    
}